package org.jcms.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jcms.model.Admin;
import org.jcms.model.Customer;
import org.jcms.model.Manager;
import org.jcms.service.AdminServiceI;
import org.jcms.service.CustomerServiceI;
import org.jcms.service.ManagerServiceI;
import org.jcms.uitils.MD5;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 添加角色
 * 
 * 参数 : username:用户名
 * 
 * password：密码
 * 
 * type 0：学生 1:教师 2：教材发行 3：仓库采购 4：管理员
 * 
 * 
 * 返回值
 * 
 * err :错误标志 0为登陆成功 1为登陆失败
 * 
 * msg : 登陆成功返回登陆的用户类别代码 登陆失败返回原因
 * 
 * @author 赵泽洋
 * 
 */
public class AdminAddRole extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AdminAddRole() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ApplicationContext ac = WebApplicationContextUtils
				.getRequiredWebApplicationContext(this.getServletContext());
		AdminServiceI adminServiceI = (AdminServiceI) ac
				.getBean("adminServiceImpl");
		CustomerServiceI customerServiceI = (CustomerServiceI) ac
				.getBean("customerServiceImpl");
		ManagerServiceI managerServiceI = (ManagerServiceI) ac
				.getBean("managerServiceImpl");

		String retJson = "{\"error\":@err,\"msg\":\"@msg\"}";
		String err = "1";
		String msg = "";

		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String type = request.getParameter("type");

		try {

			int Itype = Integer.parseInt(type);
			switch (Itype) {
			case 0:// 学生
				if (customerServiceI.findById(username) != null) {
					msg = "学生用户:该用户名已经存在!";

				}

				else {

					customerServiceI.create(new Customer(username, MD5.md5s(password),
							username, 1, 0));
					err = "0";
					msg = "创建学生帐户成功!";
				}
				break;

			case 1:
				if (customerServiceI.findById(username) != null) {
					msg = "教师用户:该用户名已经存在!";

				}

				else {

					customerServiceI.create(new Customer(username, MD5.md5s(password),
							username, 0, 0));
					err = "0";
					msg = "创建教师帐户成功!";
				}
				break;
			case 2:
				if (managerServiceI.findById(username) != null) {
					msg = "教材发行人员用户:该用户名已经存在!";

				}

				else {

					managerServiceI.create(new Manager(username, MD5.md5s(password),
							username, 0, 0));
					err = "0";
					msg = "创建教材发行人员帐户成功!";
				}

				break;
			case 3:
				if (managerServiceI.findById(username) != null) {
					msg = "仓库采购人员用户:该用户名已经存在!";

				}

				else {

					managerServiceI.create(new Manager(username, MD5.md5s(password),
							username, 1, 0));
					err = "0";
					msg = "仓库采购人员帐户成功!";
				}
				break;
			case 4:

				if (adminServiceI.findById(username) != null) {
					msg = "管理员用户:该用户名已经存在!";

				}

				else {

					adminServiceI.create(new Admin(username, MD5.md5s(password),
							username, 0));
					err = "0";
					msg = "管理员帐户成功!";
				}
				break;

			default:
				msg ="type错误";
				break;
			}

		}

		catch (Exception e) {
			msg ="系统错误";
		}

		finally {
			retJson = retJson.replace("@err", err);
			retJson = retJson.replace("@msg", msg);
			response.getOutputStream().write(retJson.getBytes("utf-8"));
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
